package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;

import DBFunction.UserManager;
import DTO.User;

/**
 * Servlet implementation class ServerLoadInfo
 */
public class ServerLoadInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServerLoadInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("loadAction");
		ArrayList<User> userList = new ArrayList();
		if(action.compareTo("loadInactive")==0){
			try {
				userList = UserManager.getUserListInGroup("Inactive");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			PrintWriter out = response.getWriter();
			for(int x=0;x<userList.size();x++){
				
				out.println("<tr align='center'>");
		    	out.println("<td><input name='memberSelect' class ='memberSelect' type='checkbox' value='"+userList.get(x).getId()+"'></td>");
		    	out.println("<td><a href='#' class='memberInfo' id='"+userList.get(x).getId()+"'onclick='loadinfo()'>"+userList.get(x).getFirstName()+"&nbsp;"+userList.get(x).getLastName()+"</a></td>");
		    	out.println("<td>"+userList.get(x).getPhoneNumber()+"</td>");
		    	out.println("<td>"+userList.get(x).getEmail()+"</td>");
		    	out.println("<td>"+userList.get(x).getAddress()+"</td>");
		    	out.println("</tr>");
		    }
			out.close();
		}
		if(action.compareTo("loadMember")==0){
			String inputId = request.getParameter("userId");
			User selectedUser = new User();
			try {
				selectedUser = UserManager.getUserById(inputId);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			JSONObject object = new JSONObject();
			try {
				object.put("id", selectedUser.getId());
				object.put("phoneNumber", selectedUser.getPhoneNumber());
				object.put("firstName", selectedUser.getFirstName());
				object.put("lastName", selectedUser.getLastName());
				object.put("email", selectedUser.getEmail());
				object.put("address", selectedUser.getAddress());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			PrintWriter out = response.getWriter();
			out.print(object);
			out.close();
		}
		if(action.compareTo("loadPosition")==0){
			String inputGroupName = request.getParameter("groupName");
			ArrayList<User> selectedUsers = new ArrayList();
			try {
				selectedUsers = UserManager.getUserPositionInGroup(inputGroupName);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Gson gson = new Gson();
			String object = gson.toJson(selectedUsers);
			PrintWriter out = response.getWriter();
			out.print(object);
			out.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

}
